const mongoose = require('mongoose')

const Image = mongoose.model('Image')

const Comment = mongoose.model('Comment')

const stats = () =>
Promise.all([
    Image.aggregate([{
        $group: {
            _id: 'total',
            images: {$sum: 1},
            views: {$sum: '$views'},
            likes: {$sum: '$likes'},
        }
    }]),
    Comment.count()
  ])
    .then(values => ({
      images: values[0][0] ? values[0][0].images : 0,
      views: values[0][0] ? values[0][0].views : 0,
      likes: values[0][0] ? values[0][0].likes :0,
      comments: values[1] ? values[1] : 0
   }))

module.exports = stats
